<template>
  <div class="login-container">
    <div class="login-bg"></div>
    <el-card class="login-card">
      <div class="logo-container">
        <h2>乡村旅游系统 -登录</h2>
      </div>

      <el-form ref="loginFormRef" @submit.prevent="handleLogin" :model="state.loginForm" :rules="state.loginRules">
        <el-form-item prop="username">
          <el-input v-model="state.loginForm.username" placeholder="请输入账号" prefix-icon="User" clearable />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="state.loginForm.password"
            type="password"
            show-password
            placeholder="请输入密码"
            prefix-icon="Lock"
            clearable
          />
        </el-form-item>

        <el-form-item prop="captcha">
          <div class="captcha-container">
            <el-input v-model="state.loginForm.captcha" placeholder="请输入验证码" prefix-icon="Key" clearable />
            <img :src="state.code" @click="getCaptcha" class="captcha-image" title="看不清换一张" alt="验证码" />
          </div>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" native-type="submit" :loading="state.loading" class="login-btn"> 登录 </el-button>
          <el-button plain @click="router.push('/register')" class="register-btn"> 注册账号 </el-button>
        </el-form-item>
      </el-form>

      <div class="footer-links">
        <el-link type="info">忘记密码?</el-link>
        <el-divider direction="vertical" />
        <el-link type="info">帮助中心</el-link>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import { useLoginApi } from '@/api/login'
import { generateRandomString } from '@/utils/common'

const router = useRouter()
const userStore = useUserStore()
const loginApi = useLoginApi()

const loginFormRef = ref()
const state = reactive({
  loginForm: {
    username: '',
    password: '',
    captcha: '',
    captchaKey: ''
  },
  loginRules: {
    username: [
      { required: true, message: '请输入账号', trigger: 'blur' },
      { min: 4, max: 16, message: '长度在4到16个字符', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 18, message: '长度在6到18个字符', trigger: 'blur' }
    ],
    captcha: [
      { required: true, message: '请输入验证码', trigger: 'blur' },
      { len: 5, message: '验证码为4位字符', trigger: 'blur' }
    ]
  },
  loading: false,
  code: ''
})

// 获取验证码
const getCaptcha = async () => {
  state.loginForm.captchaKey = generateRandomString(16)
  state.code = await loginApi.getCaptcha(state.loginForm.captchaKey)
}

// 处理登录
const handleLogin = async () => {
  try {
    await loginFormRef.value.validate()
    state.loading = true
    await userStore.toLogin(state.loginForm)
    ElMessage.success('登录成功')
    router.push('/home')
  } catch (error) {
    getCaptcha() // 验证失败刷新验证码
  } finally {
    state.loading = false
  }
}

onMounted(() => {
  getCaptcha()
})
</script>

<style scoped lang="scss">
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 50%;
    background: linear-gradient(135deg, #409eff, #67c23a);
    z-index: 1;
  }
}

.login-card {
  width: 350px;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.1);
  z-index: 2;
  position: relative;

  .logo-container {
    text-align: center;
    margin-bottom: 20px;

    .logo {
      width: 80px;
      height: 80px;
      margin-bottom: 15px;
    }

    h2 {
      color: #303133;
      font-size: 22px;
      font-weight: 600;
      margin: 0;
    }
  }

  :deep(.el-form-item__label) {
    padding-bottom: 8px;
    font-weight: 500;
  }

  .captcha-container {
    display: flex;
    align-items: center;
    gap: 10px;

    .captcha-image {
      width: 120px;
      height: 30px;
      border-radius: 4px;
      cursor: pointer;
      border: 1px solid #dcdfe6;
    }
  }

  .login-btn {
    width: 100%;
    margin-top: 10px;
  }

  .register-btn {
    width: 100%;
    margin: 0;
    margin-top: 10px;
  }

  .footer-links {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 20px;
    color: #909399;

    :deep(.el-divider) {
      margin: 0 12px;
    }

    .el-link {
      font-size: 13px;
    }
  }
}
</style>
